<?php

/**
 * This is the view;
 * @author  Womtech  email:chareler@163.com
 * @DateTime 2020-04-15 01:50
 */

use yii\helpers\Url;
use yii\helpers\Html;
use yii\bootstrap\Modal;


/* @var $this yii\web\View */
/* @var $searchModel common\models\config\ConfigBaseSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Config Bases');
//$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Xp Specials'), 'url' => ['/xpaper/xp-special/index']];//上级菜单示例
$this->params['breadcrumbs'][] = $this->title;
//CrudAsset::register($this);

$pageSize = \Yii::$app->params['pageSize'];
$limitsJson = \Yii::$app->params['limitsJson'];

?>
<!-- 面包屑 -->
<?= \Yii::$app->view->renderFile('@app/views/public/breadcrumb.php') ?>
<!-- 面包屑 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" readonly placeholder="查询加入开始时间" name="start" id="start">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" readonly placeholder="查询加入结束时间" name="end" id="end">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="title" id="title" placeholder="标题" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" type="button" id="sreach" data-type="sreach" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table" id="adminList" lay-filter="adminList"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 工具栏模板：
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
    <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
  </div>
</script> -->
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger" lay-event="delAll">批量删除</button>
        <button class="layui-btn" onclick="xadmin.open('添加用户','<?= \yii\helpers\Url::toRoute(['config-base/create']) ?>',500,550)"><i class="layui-icon"></i>添加</button>

    </div>
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="lock" value="{{d.id}}" title="是" lay-filter="lockDemo" {{ d.is_hide_des == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="checkboxStatus">
    <input type="checkbox" name="lockStatus" value="{{d.id}}" title="启用" lay-filter="lockStatus" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    $(function() {
        layui.use('laydate',
            function() {
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#start' //指定元素
                });
                //执行一个laydate实例
                laydate.render({
                    elem: '#end' //指定元素
                });
            });
    })
</script>
<?php
//$getAdminList = \yii\helpers\Url::toRoute(['/auth/get-admin-list']);
$getBaseConfigList = \yii\helpers\Url::toRoute(['get-base-config-list']);
//$update = \yii\helpers\Url::toRoute(['auth/update-admin-status']);
$update = \yii\helpers\Url::toRoute(['update']);
$delUrl = \yii\helpers\Url::toRoute(['delete']);
//$delsUrl = \yii\helpers\Url::toRoute(['deletes']);
//$addAdmin = \yii\helpers\Url::toRoute(['auth/add-admin']);
$addBaseConfig = \yii\helpers\Url::toRoute(['create']);
$_csrfBackend = \Yii::$app->request->csrfToken;
$tableJs = <<<JS
    function rowDel(obj,data,index) {
            if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                console.log(id);

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        //obj.del();
                        table.reload('adminList', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        layer.close(index);
                    }
                })
            });
       }
    function delSelected(obj,data,index){
        if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
        var id = (data.length > 0) ? getColumn(data,'id') : data.id;

        layer.confirm('真的删除吗？', function(index){
            //obj.del();
            console.log(obj);
            console.log(data);
            console.log(index);
            //layer.close(index);

            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        obj.del();
                        layer.close(index);
                    }
                })
            });

            
            /* $.each(checkedArr, function(k){
                    $(".layui-table-view .layui-table tbody  tr[data-index="+checkedArr[k]+"]").remove();
            }); */




            /* layer.close(index);
            var checkStatus = table.checkStatus("adminList");
            if(checkStatus.data.length>0){
                var arr=[];
                for(var i=0;i<checkStatus.data.length;i++){
                    arr.push(checkStatus.data[i].id);
                }
                console.log(arr);
            }else{
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            } */
        })
    }
    layui.use('table',function() {
        var table = layui.table,
            form = layui.form;
        var renderOpt = {
            id:'adminList',
            elem:'#adminList',
            toolbar:"#toolbar",
            url: '$getBaseConfigList', //数据接口
            method:"POST", //
            where:{_csrfBackend:'$_csrfBackend'},
            page: true, //开启分页
            limit: $pageSize,
            limits: $limitsJson,
             request:{
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'id', checkbox:true,title: 'ID', width:80, sort: true, fixed: 'left'},
                {field: 'title', title: '标题', minWidth:150, sort: true},
                {field: 'name', title: '标识',minWidth: 150,sort: true},
                {field: 'app_id', title: '应用', minWidth:150,sort: true},
                {field: 'type', title: '类型', minWidth:80,sort: true},
                {field: 'extra', title: '类型值', minWidth:100,sort: true},
                {field: 'description', title: '说明', minWidth:150,sort: true},
                {field: 'is_hide_des', title: '是否隐藏说明', templet: '#checkboxTpl',minWidth:120,sort: true,},
                {field: 'default_value', title: '默认配置', minWidth:150,sort: true, },
                {field: 'sort', title: '排序', minWidth:120,sort: true, },
                {field: 'status', title: '状态',templet: '#checkboxStatus', minWidth:120,sort: true, },
                {fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
        };
        table.render(renderOpt);
        //头工具栏事件
        table.on('toolbar(adminList)',
            function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                    case 'delAll':
                        var data = checkStatus.data;
                        /* rowDel(obj,data); */

                        if(data.length == 0){
                            layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                            return false;
                        }
                        var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                        layer.confirm('真的删除吗？', function(index){
                            //var url = "$delUrl";
                            //var urlArgs = {id:id} ; 
                            //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                            console.log(obj);
                            /* console.log(id);
                            console.log(obj);
                            console.log(data);
                            console.log(index);
                            console.log(table); */
                            //console.log(obj.config.page.curr);
                            
                            Cajax({
                                type:"POST",
                                url:"$delUrl",
                                data:{_csrfBackend:'$_csrfBackend',id:id}
                            },function(){},function(JsonData){
                                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                                if(JsonData.status){
                                    //obj.del(); 
                                    table.reload('adminList', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            curr: obj.config.page.curr, //当前页重新加载
                                            pageSize:obj.config.limit,
                                        }
                                    }, 'data');
                                    layer.close(index);
                                }
                            })
                        });




                        break;
                    
                    
                    
                    
                    
                    
                    case 'refresh':
                        //执行重载
                        table.reload('adminList', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        break;
                };
            });
        //监听行工具事件
        table.on('tool(adminList)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //rowDel(obj,data);
                if(data.length == 0){
                            layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                            return false;
                        }
                        var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                        layer.confirm('真的删除吗？', function(index){
                            //var url = "$delUrl";
                            //var urlArgs = {id:id} ; 
                            //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                            console.log(obj);
                            /* console.log(id);
                            console.log(obj);
                            console.log(data);
                            console.log(index);
                            console.log(table); */
                            //console.log(obj.config.page.curr);
                            
                            Cajax({
                                type:"POST",
                                url:"$delUrl",
                                data:{_csrfBackend:'$_csrfBackend',id:[id]}
                            },function(){},function(JsonData){
                                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                                if(JsonData.status){
                                    //obj.del(); 
                                    table.reload('adminList', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            page: $(".layui-laypage-skip .layui-input").val(),
                                            pageSize: $(".layui-laypage-limits").find("option:selected").val(),
                                        }
                                    }, 'data');
                                    layer.close(index);
                                }
                            })
                        });


                
            } else if(obj.event === 'edit'){
                var url = "$addBaseConfig";
                var urlArgs = {id:data.id} ; 
                xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);

                //xadmin.open('修改用户','$addAdmin?userId='+data.user_id,500,550)
            } else if(obj.event === 'member_stop'){
                member_stop(obj);
            }
        });
        form.on('checkbox(lockDemo)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:"$update",
                data:{_csrfBackend:'$_csrfBackend',id:this.value,is_hide_des:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });

        form.on('checkbox(lockStatus)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:"$update",
                data:{_csrfBackend:'$_csrfBackend',id:this.value,status:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });
        //搜索
         var $ = layui.$, active = {
            sreach: function(){
                var start = $('#start');
                var end = $('#end');
                var title = $('#title');
                //执行重载
                table.reload('adminList', {
                    //url:'$getBaseConfigList',
                    page: { curr: 1 } ,
                    where: {
                        start: start.val(),
                        end: end.val(),
                        title: title.val()
                    }
                }, 'data');
            }
          };
          $('#sreach').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
          });
     }); 
JS;
$this->registerJs($tableJs, \yii\web\View::POS_END);
?>